#include <stdio.h>
#include <string.h>
#include "build-time-get.h"

static struct build_time_info s_build_time = {0};

struct build_time_info *build_time_get(void)
{
    static const int8_t *build_time_str = __DATE__ " " __TIME__;
    static const char *months[] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    static int32_t init_flag = 0;
    int32_t i = 0;
    int8_t tmp[4] = {0};

    if(1 == init_flag)
    {
        return &s_build_time;
    }
    else
    {
        init_flag = 1;
        sscanf(build_time_str, "%3s %d %d %d:%d:%d", tmp, &s_build_time.day, &s_build_time.year, &s_build_time.hour, &s_build_time.min, &s_build_time.sec);
        for (i = 1; i < sizeof(months) / sizeof(int8_t *); i++)
        {
            if (0 == memcmp(tmp, months[i], strlen(months[i])))
            {
                s_build_time.month = i;
                break;
            }
        }
        return &s_build_time;
    }
}
